﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MAT.Model;

namespace MAT.Controllers
{
    class ctrRequestAssetMaintenance
    {
        private frmRequestAssetMaintenance _frmRequestAssetMaintenance = null;

        public frmRequestAssetMaintenance FrmRequestAssetMaintenance
        {
            get { return _frmRequestAssetMaintenance; }
            set { _frmRequestAssetMaintenance = value; }
        }

        MAT_Context context = MATContext.getContext();

        private bool check()
        {
            if (_frmRequestAssetMaintenance.txtRALReason.Text.TrimStart().Length < 1 || _frmRequestAssetMaintenance.txtRALReason.Text.TrimStart().Length > 200)
            {
                MessageBox.Show("Your reason is from 1 to 200 digits");
                _frmRequestAssetMaintenance.txtRALReason.Focus();
                return false;
            }
            else
                return true;
        }

        public void frmRequestAssetMaintenance_Load()
        {
            try
            {
                List<CATEGORY> categories = new List<CATEGORY>();
                List<ASSET> assets = new List<ASSET>();
                //List category of asset belong to user loged into MAT system
                assets = context.ASSETs.Where(ass => ass.EMPLOYEE_ID == MAT.Controllers.ctrLogin.employee_ID).ToList();
                if (assets != null)
                {
                    foreach (ASSET asset in context.ASSETs.Where(ass => ass.EMPLOYEE_ID == MAT.Controllers.ctrLogin.employee_ID).ToList())
                        categories.Add(asset.CATEGORY);
                    _frmRequestAssetMaintenance.cmbRAMAssetCategory.DataSource = categories.ToList();
                    _frmRequestAssetMaintenance.cmbRAMAssetCategory.DisplayMember = "CATEGORY_NAME";
                    _frmRequestAssetMaintenance.cmbRAMAssetCategory.ValueMember = "CATEGORY_ID";
                }
                _frmRequestAssetMaintenance.txtStartDate.Text = DateTime.Now.ToString();
            }
            catch { }
        }

        public void btnRALReset_Click()
        {
            _frmRequestAssetMaintenance.txtRALAddress.Text = "";
            _frmRequestAssetMaintenance.txtRALAssetBarcode.Text = "";
            _frmRequestAssetMaintenance.txtRALBrand.Text = "";
            _frmRequestAssetMaintenance.txtRALClassified.Text = "";
            _frmRequestAssetMaintenance.txtRALRoom.Text = "";
            _frmRequestAssetMaintenance.txtRALModel.Text = "";
            _frmRequestAssetMaintenance.txtRALReason.Text = "";
            _frmRequestAssetMaintenance.txtRALSupplier.Text = "";
            _frmRequestAssetMaintenance.cmbAssetName.Text = "";
            _frmRequestAssetMaintenance.cmbRAMAssetCategory.Text = "";
        }

        public void btnRALSend_Click()
        {
            if (check())
            {
                REQUEST_MAINTENANCE request_maintenance = new REQUEST_MAINTENANCE();
                request_maintenance.ASSET_ID = _frmRequestAssetMaintenance.cmbAssetName.SelectedValue.ToString();
                request_maintenance.STARTS_DATE = DateTime.Now;
                request_maintenance.END_DATE = null;
                request_maintenance.EMPLOYEE_ID = MAT.Controllers.ctrLogin.employee_ID;
                request_maintenance.REASON = _frmRequestAssetMaintenance.txtRALReason.Text;
                if (MAT.Controllers.ctrLogin.priority == 3 || MAT.Controllers.ctrLogin.priority == 2)
                    request_maintenance.REQ_STATUS = "RE001";
                else
                    request_maintenance.REQ_STATUS = "RE003";
                try
                {
                    context.REQUEST_MAINTENANCE.AddObject(request_maintenance);
                    context.SaveChanges();
                    MessageBox.Show("SUCCESSFUL!");
                }
                catch { }
            }
        }

        public void btnRALClose_Click()
        {
            _frmRequestAssetMaintenance.Close();
            MAT.Controllers.ctrMain._frmMain.pnlMain.Visible = false;
        }

        //This is action to reload the DataGrid when the selected value of comboBox Category is changed
        public void cmbRAMAssetCategory_SelectedValueChanged()
        {
            try
            {
                string a = _frmRequestAssetMaintenance.cmbRAMAssetCategory.SelectedValue.ToString();
                List<ASSET> assets = context.ASSETs.Where(ass => ass.EMPLOYEE_ID == MAT.Controllers.ctrLogin.employee_ID).ToList();
                assets = assets.Where(ass => ass.CATEGORY_ID == a).ToList();
                if (assets.Count != 0)
                {
                    _frmRequestAssetMaintenance.cmbAssetName.DataSource = assets.ToList();
                    _frmRequestAssetMaintenance.cmbAssetName.DisplayMember = "ASSET_NAME";
                    _frmRequestAssetMaintenance.cmbAssetName.ValueMember = "ASSET_ID";
                    _frmRequestAssetMaintenance.cmbAssetName.Enabled = true;
                }
                if (assets.Count == 0)
                {
                    _frmRequestAssetMaintenance.txtRALBrand.Text = "";
                    _frmRequestAssetMaintenance.txtRALAssetBarcode.Text = "";
                    _frmRequestAssetMaintenance.txtRALModel.Text = "";
                    _frmRequestAssetMaintenance.txtRALSupplier.Text = "";
                    _frmRequestAssetMaintenance.txtRALAddress.Text = "";
                    _frmRequestAssetMaintenance.txtRALRoom.Text = "";
                    _frmRequestAssetMaintenance.txtRALClassified.Text = "";
                    _frmRequestAssetMaintenance.cmbAssetName.Enabled = false;
                }
            }
            catch { }
        }

        public void cmbRAMAssetID_SelectedValueChanged()
        {
            try
            {
                string aID = _frmRequestAssetMaintenance.cmbAssetName.SelectedValue.ToString();
                ASSET asset = context.ASSETs.FirstOrDefault(ass => ass.ASSET_ID == aID);
                if (asset != null)
                {
                    _frmRequestAssetMaintenance.txtRALBrand.Text = asset.BRAND;
                    _frmRequestAssetMaintenance.txtRALAssetBarcode.Text = asset.BARCODE;
                    _frmRequestAssetMaintenance.txtRALModel.Text = asset.MODEL_NUM;

                    //View data of ROOM depend on Employee ID
                    string eID = asset.EMPLOYEE_ID.ToString();
                    OWNER own = context.OWNERS.FirstOrDefault(o => o.EMPLOYEE_ID == eID);
                    ROOM room = context.ROOMs.FirstOrDefault(r => r.ROOM_ID == own.ROOM_ID);
                    _frmRequestAssetMaintenance.txtRALRoom.Text = room.ROOM_NAME;
                    _frmRequestAssetMaintenance.txtRALClassified.Text = room.CLASSIFIED.ToString();

                    //View data of SUPPLIER depend on Employee ID
                    string sID = asset.SUPPLIER_ID.ToString();
                    SUPPLIER supplier = context.SUPPLIERs.FirstOrDefault(sup => sup.SUPPLIER_ID == sID);
                    _frmRequestAssetMaintenance.txtRALSupplier.Text = supplier.SUPPLIER_NAME;
                    _frmRequestAssetMaintenance.txtRALAddress.Text = supplier.SUPPLIER_ADDRESS;
                }
                _frmRequestAssetMaintenance.btnRALSend.Enabled = true;
            }
            catch { }
        }
    }
}
